<template>
  <div class="del">
    <div class="del_button" @click="confirm">确认删除</div>
    <div class="tips">
      已选测绘区域
    </div>
    <div class="wrap" v-for="v in list" :key="v.id">
      <div class="name">测绘ID: </div>
      <div class="value">{{ v.id }}</div>
      <div class="name">地块名称: </div>
      <div class="value">{{ v.land || '--' }}</div>
      <div class="del" @click="handlePolygonSelect(v.id)"></div>
    </div>
  </div>
</template>

<script setup>
import { mapState, handlePolygonSelect, clearSelectedIdList, initMapState } from "@/store";
import { computed } from "vue";
import { $toast } from "@/components/toast";
import { delPolygon } from '@/api/map'

const emits = defineEmits('getPolygons')

const list = computed(() => {
  return mapState.selectedIdList.map(id => mapState.siteList.find(v => v.id === id))
})

const confirm = async () => {
  try {
    if (!mapState.selectedIdList.length) return;
    const res = await $toast({
      title: '确认',
      text: `确认删除?`,
      leftButtonText: '取消',
      rightButtonText: '确认',
    })
    if (res === 'clickLeft') return;
    await delPolygon(mapState.selectedIdList)
    clearSelectedIdList (); 
    initMapState ()
    emits('getPolygons')
  } catch (error) {
    console.error(error);
  }
}
</script>

<style lang="scss" scoped>
.del {
  height: 5.18rem;
}

.del_button {
  width: 1.65rem;
  height: 0.52rem;
  background: rgba(255, 83, 83, 0.1);
  border-radius: 0.16rem;
  line-height: 0.52rem;
  text-align: center;
  font-size: 0.26rem;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  color: #FF5353;
  position: absolute;
  top: 0.38rem;
  right: 0.32rem;
}

.tips {
  font-size: 0.28rem;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  color: #585858;
  line-height: 0.44rem;
  margin-bottom: 0.22rem;
}

.wrap {
  display: flex;
  align-items: center;
  height: 0.84rem;
  background: #F1F1F1;
  border-radius: 0.16rem;
  margin-bottom: 0.24rem;
  padding-left: 0.2rem;
  position: relative;

  div {
    flex: 0 0 auto;

    &.name {
      font-size: 0.28rem;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      color: #7E7E7E;
      margin-right: 0.2rem;
    }

    &.value {
      font-size: 0.28rem;
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      color: #1E1E1E;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;

      &:nth-of-type(2) {
        width: 2rem;
      }
    }
  }

  .del {
    width: 0.8rem;
    height: 0.8rem;
    background-image: url('~@/assets/img/close_circle.png');
    background-size: 50%;
    background-repeat: no-repeat;
    background-position: center center;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    right: 0.25rem;
  }

}
</style>